package com.zjp.demo02251.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.omg.CORBA.portable.ResponseHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author james
 * @Title: HttpAspect
 * @Package com.zjp.demo02251.aspect
 * @Description: 日志管理
 * @date 2018/2/27  17:26
 */
@Aspect
@Component
public class HttpAspect {


    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
    /**
     * @Description: 切入点
     * @author james 添一个
     * @date 2018/2/27  17:27
    */
    @Pointcut("execution(public  * com.zjp.demo02251.controller.CompanyUserController.*(..))")
    public void log(){
    }
    /**
     * @Description: 在执行之前
     * @author james
     * @date 2018/2/27  17:51
    */
    @Before("log()")
    public void doBefour(JoinPoint joinPoint){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();

        logger.info("URL={}",request.getRequestURL());
        logger.info("IP={}",request.getRemoteAddr());
        logger.info("Method={}",request.getMethod());
        //类方法
        logger.info("class=method：{}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        logger.info("class=args:{}",joinPoint.getArgs());
    }
}

